/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package customGuiControl;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 *
 * @author emilio
 */
public class ImagePanel extends JPanel {

    private Image img=null;
    private String pathImg=null;
    private boolean drawn=false;
    private Container parent=null;
   

  public ImagePanel(String img) {
    this.pathImg=img;
    this.img=new ImageIcon(img).getImage();
  }

  public ImagePanel(Image img) {
    this.img = img;
  }

  public void setParent(Container parent)
  {
      this.parent=parent;
  }
  public Container getParent()
  {
      return this.parent;
  }


  public void paint(Graphics g) {
    //g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
        BufferedImage originalImage = null;
        try {
          
            originalImage = ImageIO.read(new File(this.pathImg));
            int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
            BufferedImage resizedImage = new BufferedImage(this.getSize().width,
                    this.getSize().height, type);
            Graphics2D g2d = (Graphics2D) g;

            /*if(this.drawn)
            g2d.clearRect(0, 0, this.getSize().width, this.getSize().height);
            //System.out.print("dsad");
            else
            this.drawn=true;*/

            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                            RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.drawImage(originalImage, 0, 0, this.getSize().width,
                    this.getSize().height, null);

            g2d.dispose();
            super.paintComponent(g2d);
        } catch (IOException e) {
            System.out.println(e.toString());
        }

  }

  
  
    public String getPathImg() {
        return pathImg;
    }

    public void setPathImg(String pathImg) {
        this.pathImg = pathImg;
    }

    public static BufferedImage toBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage)image;
        }

        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();

        // Determine if the image has transparent pixels; for this method's
        // implementation, see Determining If an Image Has Transparent Pixels
        boolean hasAlpha=true;

        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
            if (hasAlpha) {
                transparency = Transparency.BITMASK;
            }

            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(
                image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {
            // The system does not have a screen
        }

        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha) {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }

        // Copy image to buffered image
        Graphics g = bimage.createGraphics();

        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bimage;
  }
}

